package com.ellison.springdemo.config;

/**
 * <p>TODO</p>
 *
 * @author Ellison_Pei
 * @date 2022/4/6 18:25
 * @since 1.0
 **/

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
        @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
@Component
@Slf4j
// 配置文件读取是否启用此配置
@ConditionalOnProperty(prefix = "slow-sql-interceptor", name = "enabled", havingValue = "true")
public class SqlExecuteTimeInterceptor implements Interceptor {

    // 日志记录mapper
    //@Autowired
    //private LogMapper logMapper;

    /**  是否插入数据库标识 */
    @Value("${slow-sql.insertDB}")
    private boolean isInsertDb;

    /** 超时时间 */
    @Value("${slow-sql.timeout}")
    private Long timeout;

    /**
     *  sql拦截器
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget();
        long begin = System.currentTimeMillis();
        StatementHandler statementHandler = (StatementHandler) target;
        try {
            return invocation.proceed();
        } finally {
            long end = System.currentTimeMillis();
            // 判断超时
            if ((end - begin) > timeout) {
                // 方法名称、类型
                MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
                MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
                String methodName = mappedStatement.getId();
                String sqlType = mappedStatement.getSqlCommandType().toString();
                // 控制台打印日志
                BoundSql boundSql = statementHandler.getBoundSql();
                String sql = boundSql.getSql();
                // 参数map
                Object parameterObject = boundSql.getParameterObject();
                // 参数列表
                List<ParameterMapping> parameterMappingList = boundSql.getParameterMappings();
                // 格式化sql语句，去除换行符，替换参数
                sql = formatSQL(sql, parameterObject, parameterMappingList);
                log.error("执行 SQL：[ {} ], 执行耗时[ {} ms ]", sql, (end - begin));
                // 判断是否写入数据库
                if (isInsertDb) {
                    // 转换参数map为String
                    //String params = changeSqlMapParams(parameterObject, parameterMappingList);
                    //SqlLog log = new SqlLog();
                    //log.setMethodName(methodName);
                    //log.setParams(params);
                    //log.setRunTime((end - begin));
                    //log.setExecuteSql(sql);
                    //log.setSqlType(sqlType);
                    //logMapper.insertSqlLog(log);
                }
            }
        }
    }

    private String changeSqlMapParams(Object parameterObject, List<ParameterMapping> parameterMappingList) {
        StringBuilder sb = new StringBuilder();
        Map<String, Object> params = (Map<String, Object>) parameterObject;
        for (ParameterMapping pm : parameterMappingList) {
            if (pm.getMode().name().equals("IN")) {
                String param = ",[" + params.get(pm.getProperty()).toString() + "]";
                sb.append(param);
            }
        }
        return sb.length() > 0 ? sb.substring(1) : null;
    }

    private String formatSQL(String sql, Object parameterObject, List<ParameterMapping> parameterMappingList) {
        if (sql == null || sql.length() == 0) {
            return "";
        }
        // 去除换行符
        sql = sql.replaceAll("[\\s\n ]+", "  ");
        // 替换参数
        Map<String, Object> params = (Map<String, Object>) parameterObject;
        for (ParameterMapping pm : parameterMappingList) {
            if (pm.getMode().name().equals("IN")) {
                sql = sql.replaceFirst("\\?", params.get(pm.getProperty()).toString());
            }
        }
        return sql;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
